import React from 'react';
import {Table} from 'antd';
import PropTypes from 'prop-types';
import routes from './routes';
import {Link} from 'react-router-dom';
import {PAGINATION_OPTIONS, TABLE, COMMON_TABLE_QUERY_FAIL_TEXT, COMMON_TABLE_EMPTY_TEXT, DATETIME_FORMAT} from '../constants';
import {injectIntl} from './intl';
import {conventEnumValueToString, formatDateTime, formatAmount} from '../utils';
import {paymentType, partContractInvoiceStatus, transportType, partOrderPriceTerms} from '../Enum';
import {hasPermissions} from './utils';
import {PERMISSION} from './constants';
export class TablePanel extends React.PureComponent {
    constructor(props) {
        super(props);
        this.onChangeSort = this.onChangeSort.bind(this);
        this.state = {
            loading: false
        };
    }

    onChangeSort = (pagination, filters, sorter) => {
        this.props.onChangeSort({
            pageIndex: pagination.current - 1,
            pageSize: pagination.pageSize,
            sortedColumnKey: sorter.columnKey,
            sortedOrder: sorter.order,
        });
    };
    render() {
        const colums = [
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.code',
                    defaultMessage: '发票编号'
                }),
                dataIndex: 'code',
                width: '100px',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'code' && this.props.sortedOrder,
                render: (text, record) => hasPermissions(record.options, PERMISSION.details)
                    ? <Link to={routes.detail.format(record.id)} >{text}</Link> : text
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.dealerCode',
                    defaultMessage: '总代编号'
                }),
                dataIndex: 'dealerCode',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'dealerCode' && this.props.sortedOrder
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.partSaleOrderCodes',
                    defaultMessage: '订单编号'
                }),
                dataIndex: 'partSaleOrderCodes'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.sapOrderCode',
                    defaultMessage: 'SAP销售订单号'
                }),
                dataIndex: 'sapOrderCode'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.shipmentType',
                    defaultMessage: '运输方式'
                }),
                dataIndex: 'shipmentType',
                render: text => conventEnumValueToString(transportType, text),
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'shipmentType' && this.props.sortedOrder
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.paymentType',
                    defaultMessage: '付款方式'
                }),
                dataIndex: 'paymentType',
                render: text => conventEnumValueToString(paymentType, text),
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'paymentType' && this.props.sortedOrder
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.priceTerms',
                    defaultMessage: '价格条款'
                }),
                dataIndex: 'priceTerms',
                render: text => conventEnumValueToString(partOrderPriceTerms, text),
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'priceTerms' && this.props.sortedOrder
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.totalAmount',
                    defaultMessage: 'FOB金额'
                }),
                dataIndex: 'totalAmount',
                render: text => formatAmount(text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.invoiceAmount',
                    defaultMessage: '发票金额'
                }),
                dataIndex: 'invoiceAmount',
                render: text => formatAmount(text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.freight',
                    defaultMessage: '运费'
                }),
                dataIndex: 'freight',
                render: text => formatAmount(text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.insurance',
                    defaultMessage: '保费'
                }),
                dataIndex: 'insurance',
                render: text => formatAmount(text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.totalDiscount',
                    defaultMessage: '折扣总金额'
                }),
                dataIndex: 'totalDiscount',
                render: text => formatAmount(text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.itemCount',
                    defaultMessage: '备件品种数'
                }),
                dataIndex: 'itemCount'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.totalQuantity',
                    defaultMessage: '备件总数量'
                }),
                dataIndex: 'totalQuantity'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.status',
                    defaultMessage: '状态'
                }),
                dataIndex: 'status',
                render: text => conventEnumValueToString(partContractInvoiceStatus, text),
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'status' && this.props.sortedOrder
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.invoicedDate',
                    defaultMessage: '开票日期'
                }),
                dataIndex: 'invoicedDate',
                render: text => formatDateTime(text, DATETIME_FORMAT),
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'invoicedDate' && this.props.sortedOrder
            }];
        const pagination = {
            total: this.props.total,
            pageSize: this.props.pageSize,
            current: this.props.pageIndex + 1,
            ...PAGINATION_OPTIONS
        };
        const locale = {
            emptyText: this.props.errorMessage ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        return (
            <div>
                <Table
                    className="white-space-nowrap"
                    columns={colums}
                    dataSource={this.props.list}
                    onChange={this.onChangeSort}
                    rowKey="id"
                    pagination={pagination}
                    loading={this.props.loading || this.state.loading}
                    locale={locale}
                    {...TABLE} />
            </div>
        );
    }
}

TablePanel.propTypes = {
    errorMessage: PropTypes.string.isRequired,
    list: PropTypes.array.isRequired,
    loading: PropTypes.bool.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    intl: PropTypes.object,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
    sortedColumnKey: PropTypes.string,
    sortedOrder: PropTypes.string,
};

import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {tableSearch} from './actions';

const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const mapStateToProps = state => ({
    list: getData(state),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    errorMessage: state.getIn(['page', 'domainData', 'list', 'message']),
    sortedColumnKey: state.getIn(['page', 'appState', 'queryBySort', 'sortedColumnKey']),
    sortedOrder: state.getIn(['page', 'appState', 'queryBySort', 'sortedOrder'])
});

const mapDispatchToProps = dispatch => ({
    onChangeSort: option => dispatch(tableSearch(option))
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TablePanel));
